package org.df.ulab.bot.engine.commands;

import java.net.URL;
import java.util.Iterator;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.df.ulab.bot.api.command.CommandPattern;
import org.df.ulab.bot.api.communication.CommunicationService;
import org.df.ulab.bot.api.core.Engine;
import org.df.ulab.bot.api.message.Message;
import org.df.ulab.bot.util.command.AbstractCommand;
import org.df.ulab.bot.util.core.EngineSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author Daniel Filgueiras
 * @since 19/06/2011
 */
@CommandPattern(patterns={"^//Status"})
public class SystemStatus extends AbstractCommand {

	private static final Logger logger = LoggerFactory.getLogger(SystemStatus.class);
	
	@Override
	public boolean proccess(Message message) {
		
		StringBuilder builder = new StringBuilder();
		
		Engine engine = EngineSession.getEngine();
		CommunicationService service = EngineSession.getCommunicationService();
		
		builder
			.append("Engine Id:\t ").append(engine.getId()).append("\r\n")
			.append("Msg Count:\t ").append(engine.getMessageCount()).append("\r\n")
			.append("Service:\t ").append(service.getClass().getSimpleName()).append("\r\n")
			.append("Income#:\t ").append(service.getIncome().size()).append("\r\n");
		
		try {
			URL url = getClass().getResource("/META-INF/MANIFEST.MF");
			if (url != null) {
				Configuration config = new PropertiesConfiguration(url);
				
				Iterator<?> keys = config.getKeys();
				while (keys.hasNext()) {
					String key = keys.next().toString();
					String value = config.getString(key);
					builder.append(key);
					builder.append(":\t ");
					builder.append(value);
					builder.append("\r\n");
				}
				
			}
		} catch (Exception e) {
			logger.error("Unable to read manifest file and get build revision");
		}
		
		service.reply(message, builder);
		
		return true;
	}

}
